/* 程序12-13 readn函数 */
#include <unistd.h>
#include <stdio.h>

ssize_t readn(int fd, void *vptr, size_t n)
{
	size_t nleft;
	ssize_t nread;
	char *ptr;

	ptr = vptr;
	nleft = n;
	while (nleft > 0) {
		if ((nread = read(fd, ptr, nleft)) < 0)
			return(nread);		/* error, return < 0 */
		else if (nread == 0)	
			break;				/* EOF */

		nleft -= nread;
		ptr += nread;
	}
	return(n - nleft);		/* return >= 0 */
}
